<?PHP
 session_start();
 include("admin/include/db.php");
 include("includes/function.php");

 if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'Login'){
	 
	$user_email 	= mysql_real_escape_string($_REQUEST['login_email']);
	$user_password 	= mysql_real_escape_string($_REQUEST['login_password']);
	
	$sqlLogin 		= mysql_query("SELECT id,email,first_name,last_name ,user_type
										FROM software_user WHERE email = '$user_email' AND password = '$user_password ' AND active  = 'Y'");
 	$resLogin		= mysql_fetch_array($sqlLogin);
	$user_id		= intval($resLogin["id"]);
	
	if($user_id > 0){
	
		$_SESSION['user_id']			= $user_id;
		$_SESSION['user_email']			= $resLogin["email"];
		$_SESSION['user_type']			= $resLogin["user_type"];
		$_SESSION['user_first_name']	= $resLogin["first_name"] ;
		
		if($_SESSION['user_type'] == 'Student')
		{
			header("location:std_profile_update.php");
		}elseif($_SESSION['user_type'] == 'Trainer'){
			header("location:trainer_profile_update.php");
		}elseif($_SESSION['user_type'] == 'Training Institute'){
			header("location:institute_profile_update.php");
		}
		exit();
	}else{
		header("location:login.php?msg=3");
		exit();
	}
 }
 
  if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'Register'){
	
	$user_type 		= mysql_real_escape_string($_REQUEST['user_type']); 
	$user_course	= $_REQUEST['user_course'];
	$user_email 	= mysql_real_escape_string($_REQUEST['user_email']);
	$user_password 	= mysql_real_escape_string($_REQUEST['user_password']);
	$first_name 	= mysql_real_escape_string($_REQUEST['first_name']);
	$last_name 		= mysql_real_escape_string($_REQUEST['last_name']);
	$user_mobile 	= mysql_real_escape_string($_REQUEST['user_mobile']);
	$user_address 	= mysql_real_escape_string($_REQUEST['user_address']);
	$user_city 		= mysql_real_escape_string($_REQUEST['user_city']);
	$user_state 	= mysql_real_escape_string($_REQUEST['user_state']);
	$user_country 	= mysql_real_escape_string($_REQUEST['user_country']);
	$user_comments 	= mysql_real_escape_string($_REQUEST['user_comments']);
	$user_zipcode 	= mysql_real_escape_string($_REQUEST['user_zipcode']);
	$active 		= mysql_real_escape_string($_REQUEST['active']);
	$dupSql			= "SELECT
							*
						FROM
							software_user
						WHERE
							email = '".$user_email."'";
							
	$checkDupRes	=	mysql_query($dupSql);
	if(mysql_num_rows($checkDupRes)>0)
	{
		header("location:login.php?msg=2");
		exit();	
	}
	else
	{
		$sqlCategory = "INSERT INTO software_user
								 SET 
							email					=	'$user_email',
							password				=	'$user_password',
							address					=	'$user_address',
							city					=	'$user_city',
							state					=	'$user_state',
							country					=	'$user_country',
							user_type				=	'$user_type',
							user_creation_date		=	UNIX_TIMESTAMP(),
							user_expirey_date		=	'$user_email',
							active					=	'Y',
							user_comments			=	'$user_comments',
							first_name				=	'$first_name',
							last_name				=	'$last_name',
							mobile					=	'$user_mobile',
							user_zipcode			=	'$user_zipcode'
							"
							; 	
	if(mysql_query($sqlCategory))
	{
		$user_id = mysql_insert_id();
		if(!empty($user_course))
		{
			foreach($user_course as $courses){
				$course			 = "INSERT INTO software_user_courses SET user_course = '$courses',user_type = '$user_type',user_id = '$user_id',course_date=UNIX_TIMESTAMP()";
				$userCourseAdded = mysql_query($course);	
			}
		}
		
		$to		 =	$user_email.',greatravians@gmail.com';
		$from 	 = "no-reply@elearning.com";
		$subject = "Confirm your registration at elearning.com!";
		
		$emailMessage = '
						<style type="text/css">
							.text {font-family: Arial, Helvetica, sans-serif; font-size: 12px; }						
						</style>
						<table width="100%" cellpadding="2" cellspacing="2" class="text">																	
							<tr>
								<td colspan="2" align="left">&nbsp;</td>
							</tr>
							<tr>
								<td colspan="2" align="left">Congratulations! </td>
							</tr>
							<tr>
								<td colspan="2" align="left" style="padding-top:10px">You are now becoming a member of elarningsystem.com!</td>
							</tr>
							<tr>
								<td colspan="2" align="left" style="padding-top:10px">Please click on the following link <a href="'. WEBSITE_URL .'activateYourLink.php?user_id='.$user_id.'&email='.$user_email.'">Activate your link</a> elarningsystem.com!</td>
							</tr>
							<tr>
								<td colspan="2" align="left" style="padding-top:10px">Congragulations,your can now successfully login in our system</td>
							</tr>
						</table>';
		
		 emailFormat($to , $from , $subject , $emailMessage);
		
		header("location:login.php?msg=1");
		exit();	
		
		}
			
 	}
 }
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=TITLE_WEBSITE?></title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<script>
function login_validation(){
	
	var message = '';
	var emailLogin	  = document.getElementById("login_email").value;
	var passwordLogin = document.getElementById("login_password").value;
	
	if(emailLogin == ''){
		message += 'Please enter email address \n';	
	}
	
	if(emailLogin != ""){
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    	var address = emailLogin;
    	if(reg.test(address) == false) {
 			message += 'Please enter valid email address\n';
      	}
	}
	
	if(passwordLogin == ''){
		message += 'Please enter your password\n';	
	}
	
	if(message != ""){
		alert(message);
		return false;	
	}
}
function register_validation(){
	
	var message = '';
	var emailReg	  = document.getElementById("user_email").value;
	var passwordReg   = document.getElementById("user_password").value;
	var first_name    = document.getElementById("first_name").value;
	var last_name     = document.getElementById("last_name").value;
	var user_mobile   = document.getElementById("user_mobile").value;
	var user_zipcode  = document.getElementById("user_zipcode").value;
	var user_country  = document.getElementById("user_country").value;

	var user_type     = document.getElementById("user_type").value;
	
	
	
	if(emailReg == ''){
		message += 'Please enter email address \n';	
	}
	
	if(emailReg != ""){
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    	var address = emailReg;
    	if(reg.test(address) == false) {
 			message += 'Please enter valid email address\n';
      	}
	}
	
	if(passwordReg == ''){
		message += 'Please enter your password\n';	
	}
	
	if(first_name == ""){
		message += 'Please enter your first name\n';
	}
	if(last_name == ""){
		message += 'Please enter your last name\n';
	}
	if(emailReg == ""){
		message += 'Please enter your user email\n';
	}
	
	if(user_country == ""){
		message += 'Please select your country\n';
	}
	if(user_type == ""){
		message += 'Please select your user type\n';
	}
	if(user_mobile == ""){
		message += 'Please enter your mobile\n';
	}

	if(user_zipcode == ""){
		message += 'Please enter your zip code\n';
	}
	
	
	if(message != ""){
		alert(message);
		return false;	
	}
}


</script>
<body>
<center>
<div style="background:#179cc1; height:3px">&nbsp;</div>
<div style="width:990px;" align="center">
		<div id="header">
		<?php
    		include("includes/header.php");
   		?>
        </div>
    <div style="margin-top:20px;">
    	<form id="form_login" name="form_login" method="post" onsubmit="return login_validation()" >
    	<?php
		$msg = $_REQUEST['msg'];
        if($msg != "")
		{
			if($msg == 1){
				$showMessage = 'You are registered successfully,please check your email for activation';
				$clsMessage = 'clsSuccess';	
			}else if($msg == 2){
				$showMessage = 'Sorry,Please check your email address again,as email address is already exist';
				$clsMessage = 'clsError';
			}else if($msg == 3){
				$showMessage = 'Sorry,Please check your email and password for login';
				$clsMessage = 'clsError';
			}else if($msg == 4){
				$showMessage = 'Congragulations,Please login to our website';
				$clsMessage = 'clsSuccess';
			}else if($msg == 5){
				$showMessage = 'Sorry,Please check your confirm password is not matching';
				$clsMessage = 'clsError';
			}
		?>
        <div align="left" class="<?=$clsMessage?>" style="margin-bottom:10px" >
           &nbsp; 	 <?=$showMessage?>
        </div>
        <?php
		}
		?>
        <div style="float:left; width:50%; float:left" >
        	<div align="left" >
            	<span class="headingOrange"><img src="images/login.png" width="40" align="absbottom"  /><strong> Login</strong></span>
            </div>
            <div class="text1"  align="left" style=" padding-top:18px;">
                <div  >	
                    <div style="float:left; width:30%" class="text" >Email:</div>
                    <div style="float:left; width:70%" ><input type="text" class="textBox" id="login_email" name="login_email" value=""  /> </div>
                    <div style=" clear:both" ></div>
                </div>
                 <div style="margin-top:10px" >	
                    <div style="float:left; width:30%" class="text" >Password:</div>
                    <div style="float:left; width:70%" ><input type="password" class="textBox"  id="login_password" name="login_password" value=""  /> </div>
                    <div style=" clear:both" ></div>
                </div>
                <div style="margin-top:10px;" align="center">
                	<input type="hidden" id="action" name="action" value="Login"  />
                   	<input type="submit" class="button" value="Login"  /> &nbsp;<a href="forgot_password.php" class="menuTrayLink">Forgot your password</a>
                </div>
             </div>
               <div align="left" style="margin-top:50px">
        		<?php
									$sqlAdvertise = mysql_query("SELECT * FROM software_advertise 
																	WHERE 
																		banner_status = 'Y' AND banner = 3
																		ORDER BY RAND()
																		");
									$resAdvertise = mysql_fetch_array($sqlAdvertise);
							
									$imgPath  = 'admin/upload_images/'.$resAdvertise["banner_image"];
									
								?>
								<!--<a href="http://<?=$resAdvertise["banner_url"]?>" target="_blank" ><img border="0" src="<?=$imgPath?>"   /></a>-->
        </div>
        </div>
        
        </form>
        <form id="form_register" name="form_register" method="post" onsubmit="return register_validation()" >
        <div style="float:left; width:50%;" >
        	<div align="left" >
            	<span class="headingOrange" ><img src="images/register.png" width="40" align="absbottom"  /><strong>Register for free</strong></span>
            </div>
            <div class="text1"  align="left" style=" padding-top:18px;">
         		 <div  >	
                    <div style="float:left; width:30%" class="text" >Email:</div>
                    <div style="float:left; width:70%" ><input type="text" id="user_email" class="textBox" name="user_email" value="" maxlength="30"  /> </div>
                    <div style=" clear:both" ></div>
                </div>
                <div  style="margin-top:10px" >	
                    <div style="float:left; width:30%" class="text" >Password:</div>
                    <div style="float:left; width:70%" ><input type="password" id="user_password" class="textBox" name="user_password" value="" maxlength="30"  /> </div>
                    <div style=" clear:both" ></div>
                </div>
                <div  style="margin-top:10px"  >	
                    <div style="float:left; width:30%" class="text" >User Type:</div>
                    <div style="float:left; width:70%" >
                    	<select id="user_type" name="user_type" class="textBox" >
                        	<option value="" >Select</option>
                            <option value="Student" >Student</option>
                            <option value="Trainer" >Trainer</option>
                        </select>
                    </div>
                    <div style=" clear:both" ></div>
                </div>
                <div  style="margin-top:10px" >	
                    <div style="float:left; width:30%" class="text" >Couses:</div>
                    <div style="float:left; width:70%" >
                    	<select id="user_course[]" name="user_course[]" multiple="multiple" class="multiSelect">
                        	<?php
                           	$sqlCourse = mysql_query("SELECT * FROM software_main_course where active = 'Y'");
							while($resCourse = mysql_fetch_array($sqlCourse )){
						   ?>
                           	<option value="<?php echo $resCourse["course_name"]?>" ><?php echo $resCourse["course_name"]?></option>
                           
						   <?php			
							}
						   ?>
                        </select>
                    </div>
                    <div style=" clear:both" ></div>
                </div>
                <div style="margin-top:10px"  >	
                    <div style="float:left; width:30%" class="text" >First Name:</div>
                    <div style="float:left; width:70%" ><input onkeypress="return alphabetsOnly(this, event)" type="text" id="first_name" class="textBox" name="first_name" value=""  maxlength="30" /> </div>
                    <div style=" clear:both" ></div>
                </div>
                <div  style="margin-top:10px" >	
                    <div style="float:left; width:30%" class="text" >Last Name:</div>
                    <div style="float:left; width:70%" ><input onkeypress="return alphabetsOnly(this, event)" type="text" id="last_name" class="textBox" name="last_name" value="" maxlength="30"  /> </div>
                    <div style=" clear:both" ></div>
                </div>
                 <div  style="margin-top:10px" >	
                    <div style="float:left; width:30%" class="text" >Mobile:</div>
                    <div style="float:left; width:70%" ><input onkeypress="return intOnly(this, event)" type="text" id="user_mobile" class="textBox" name="user_mobile" value=""  maxlength="30" /> </div>
                    <div style=" clear:both" ></div>
                </div>
               
                <div  style="margin-top:10px" >	
                    <div style="float:left; width:30%" class="text" >Address:</div>
                    <div style="float:left; width:70%" ><input type="text" id="user_address" class="textBox" name="user_address" value=""   maxlength="30"/> </div>
                    <div style=" clear:both" ></div>
                </div> 
                <div style="margin-top:10px"  >	
                    <div style="float:left; width:30%" class="text" >City:</div>
                    <div style="float:left; width:70%" ><input onkeypress="return alphabetsOnly(this, event)" type="text" id="user_city" class="textBox" name="user_city" value=""  maxlength="30"  /> </div>
                    <div style=" clear:both" ></div>
                </div>
                 <div style="margin-top:10px"  >	
                    <div style="float:left; width:30%" class="text" >State:</div>
                    <div style="float:left; width:70%" ><input type="text" onkeypress="return alphabetsOnly(this, event)" id="user_state" class="textBox" name="user_state" value=""  maxlength="30"  /> </div>
                    <div style=" clear:both" ></div>
                </div>
                 <div style="margin-top:10px"  >	
                    <div style="float:left; width:30%" class="text" >Zip Code:</div>
                    <div style="float:left; width:70%" ><input onkeypress="return intOnly(this, event)" type="text" class="textBox" id="user_zipcode" name="user_zipcode" value=""  maxlength="30"  /> </div>
                    <div style=" clear:both" ></div>
                </div>
                 <div style="margin-top:10px" >	
                    <div style="float:left; width:30%" class="text" >Country:</div>
                    <div style="float:left; width:70%" ><select id="user_country" name="user_country" style="width:220px" class="textBox" >
                        	<option value="" >Select</option>
							<?php
                            	$sqlCountry = mysql_query("SELECT * FROM software_country WHERE status = 'Y'");
								while($resCountry = mysql_fetch_array($sqlCountry)){
							?>
                            <option value="<?php echo $resCountry["country_code"]; ?>" ><?php echo $resCountry["country_name"]; ?></option>
                            <?php
								}
							?>
                        </select>
                      </div>
                    <div style=" clear:both" ></div>
                </div>
                
                 <div style="margin-top:10px;" align="center">	
                 	<input type="hidden" id="action" name="action" value="Register"  />
                   	<input type="submit" class="button" value="Register"  />
                </div>
                 
            </div>
        </div>
        </form>
      <div style="clear:both"></div>
    </div>
    
    <div style="margin-top:23px; margin-bottom:21px" align="center" >
    	<?php
									$sqlAdvertise = mysql_query("SELECT * FROM software_advertise 
																	WHERE 
																		banner_status = 'Y' AND banner = 2
																		ORDER BY RAND()
																		");
									$resAdvertise = mysql_fetch_array($sqlAdvertise);
							
									$imgPath  = 'admin/upload_images/'.$resAdvertise["banner_image"];
									
								?>
								
    </div>
   </div>
   <div id="footer">
  		<?php include("includes/footer.php")?>
   </div>
</div>
	</div>
</center>
</body>
</html>
